package com.example.elderlycare.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.elderlycare.controller.entity.EquipTypeRequestBody;
import com.example.elderlycare.dao.entity.EquipType;
import com.example.elderlycare.service.EquipTypeService;
import com.example.elderlycare.service.entity.EquipTypeListResponseBody;
import com.example.elderlycare.utils.Result;
import com.example.elderlycare.utils.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

@RestController
@RequestMapping("/equipType")
public class EquipTypeController {
    private final Logger logger = LoggerFactory.getLogger(EquipmentController.class);

    @Resource
    EquipTypeService equipTypeService;


    /**
     * queryEquipTypeList 查询设备类型列表
     *
     * @param name     类型名称
     * @param upType   所属大类
     * @param isMain   是否是主机
     * @param pageSize 页面容量
     * @param current  当前页码
     * @return EquipTypeListResponseBody
     */
    @GetMapping
    public Result<EquipTypeListResponseBody> queryEquipTypeList(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "upType", required = false) String upType,
            @RequestParam(value = "isMain", required = false) String isMain,
            @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
            @RequestParam(defaultValue = "1", value = "current") int current
    ) {
        QueryWrapper<EquipType> queryWrapper = new QueryWrapper<>();
        if (name != null && !"".equals(name)) {
            queryWrapper.like("name", name);
        }
        if (upType != null && !"".equals(upType)) {
            queryWrapper.like("up_type", upType);
        }
        if (isMain != null && !"".equals(isMain)) {
            queryWrapper.like("is_main", isMain);
        }
        EquipTypeListResponseBody responseBody = equipTypeService.queryEquipTypeList(current, pageSize, queryWrapper);
        if (responseBody == null) {
            Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(responseBody);
    }


    /**
     * addEquipType 增加设备类型
     *
     * @param req EquipTypeRequestBody.AddEquipTypeRequestBody
     * @return null
     */
    @PostMapping
    public Result<?> addEquipType(@Valid @RequestBody EquipTypeRequestBody.AddEquipTypeRequestBody req) {
        EquipType equipType = EquipType.builder().
                name(req.getName()).
                upType(req.getUpType()).
                isMain("是".equals(req.getIsMain()) ? 1 : 0).
                build();
        boolean success = equipTypeService.save(equipType);
        if (success) {
            return Result.success(null);
        } else {
            logger.warn("add equipType error");
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }


    /**
     * updateEquipType 根据id更新类型信息
     *
     * @param req EquipTypeRequestBody.UpdateEquipTypeRequestBody
     * @return null
     */
    @PutMapping
    public Result<?> updateEquipType(@Valid @RequestBody EquipTypeRequestBody.UpdateEquipTypeRequestBody req) {
        EquipType equipType = EquipType.builder().
                id(req.getId()).
                name(req.getName()).
                upType(req.getUpType()).
                isMain("是".equals(req.getIsMain()) ? 1 : 0).
                build();
        boolean success = equipTypeService.updateById(equipType);
        if (success) {
            return Result.success(null);
        } else {
            logger.warn("update equipType error");
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }


    /**
     * deletEquipType 根据id进行删除
     *
     * @param id 需要删除记录的id
     * @return null
     */
    @DeleteMapping
    public Result<?> deletEquipType(@RequestParam("id") @NotEmpty Integer id) {
        boolean success = equipTypeService.removeById(id);
        if (success) {
            return Result.success(null);
        } else {
            logger.warn("delete equipType error");
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }
}
